package com.rockbb.common.simpleapp;

import org.apache.log4j.Logger;

public class ThreadBasic extends Thread {

	protected static Logger logger = Logger.getLogger(ThreadBasic.class);

	/**
	 * 获取当前执行绪的编号, 根据指定的执行绪名称前缀
	 * 
	 * @param prefix
	 * @return
	 */
	public int fetchExecutorSequence(String prefix) {
		String index = Thread.currentThread().getName().substring(prefix.length() + 1);
		int sequence;
		try {
			sequence = Integer.parseInt(index);
		} catch (NumberFormatException e) {
			logger.debug("fetchExecutorSequence error: " + index + " is not an integer");
			sequence = 0;
		}
		return sequence;
	}

	/**
	 * 获取当前执行绪的编号, 如果当前执行中包含多个执行绪池, 则需要指定执行绪前缀
	 */
	public int fetchExecutorSequence() {
		String thread_name = Thread.currentThread().getName();
		int pos = thread_name.lastIndexOf("-");
		String index = Thread.currentThread().getName().substring(pos + 1);
		int sequence;
		try {
			sequence = Integer.parseInt(index);
		} catch (NumberFormatException e) {
			logger.debug("fetchExecutorSequence error: " + index + " is not an integer");
			sequence = 0;
		}
		return sequence;
	}
}
